-- utf8
vim.g.encoding = "UTF-8"
vim.o.fileencodings = "utf-8,ucs-bom,GB2312,big5,litan1"

-- jkhl 移动时光标周围保留8行
vim.o.scrolloff = 8
vim.o.sidescrolloff = 8

-- 使用相对行号
vim.wo.number = true
vim.wo.relativenumber = true

-- 高亮所在行
vim.wo.cursorline = true

-- 显示左侧图标指示列
vim.wo.signcolumn = "yes"

-- 右侧参考线，超过表示代码太长了，考虑换行
vim.wo.colorcolumn = "100"

-- 缩进4格表示一个tab
vim.o.tabstop = 4
vim.bo.tabstop = 4
vim.o.softtabstop = 4
vim.o.shiftround = true

-- >> << 时移动长度
vim.o.shiftwidth = 4
vim.bo.shiftwidth = 4

-- 空格替代tab
vim.o.expandtab = true
vim.bo.expandtab = true

-- 新行对齐当前行
vim.o.autoindent = true
vim.bo.autoindent = true
vim.o.smartindent = true

-- 搜索大小写不敏感，除非包含大写
vim.o.ignorecase = true
vim.o.smartcase = true

-- 搜索高亮
vim.o.hlsearch = true

-- 边输入边搜索
vim.o.incsearch = true

-- 命令行高为2
vim.o.cmdheight = 2

-- 当文件被外部程序修改时，自动加载
vim.o.autoread = true
vim.bo.autoread = true

-- 禁止折行
vim.wo.wrap = false

-- 光标在行首尾时<Left><Right>可以跳到下一行
vim.o.whichwrap = "<,>,[,]"

-- 允许隐藏被修改过的的buffer
vim.o.hidden = true

-- 鼠标支持
vim.o.mouse = "a"

-- 禁止创建备份文件
vim.o.backup = false
vim.o.writebackup = false
vim.o.swapfile = false

-- smaller updatetime
vim.o.updatetime = 300

-- 设置肩旁快捷键连击时间500毫秒，可根据需要设置
vim.o.timeoutlen = 500

-- split window 从下边和右边出现
vim.o.splitbelow = true
vim.o.splitright = true

-- 自动补全不自动选中
vim.g.completeopt = "menu, menuone, noselect, noinsert"

-- 样式
vim.o.background = "dark"
vim.o.termguicolors = true
vim.opt.termguicolors = true

-- 不可见字符显示
vim.o.list = true
vim.o.listchars = "tab:→ ,space:·"

-- 补全增强
vim.o.wildmenu = true

-- Don't pass messages to |ins-completin menu|
vim.o.shortmess = vim.o.shortmess .. "c"

-- 补全最多显示10行
vim.o.pumheight = 10

-- 永远显示tabline
vim.o.showtabline = 4

-- 使用增强状态栏插件后不再需要vim的模式提示
vim.o.showmode = false

-- 离开插入模式/离开当前buffer/输入命令 时自动保存文件
vim.api.nvim_create_autocmd({ "InsertLeave", "BufLeave", "CmdlineEnter" }, {
	callback = function()
		vim.fn.execute("silent! write")
		-- vim.notify("Autosaved!", vim.log.levels.INFO, {})
	end,
})

-- 共用剪切板
-- vim.o.clipboard = "unnamedplus"

local is_wsl = vim.fn.has("wsl") == 1
local is_mac = vim.fn.has("macunix") == 1
local is_linux = vim.fn.has("unix") == 1 and not is_wsl and not is_mac

if is_wsl then
	-- WSL: 使用 Windows 剪贴板桥接
	local function executable_in_wsl(path)
		return vim.fn.executable(path) == 1
	end

	-- 推荐路径：把 win32yank.exe 放在某个固定位置，比如 C:\tools\
	local win32yank = "/mnt/c/tools/win32yank.exe"
	local clip = "/mnt/c/Windows/System32/clip.exe"
    local powershell = "/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe"

	if executable_in_wsl(win32yank) then
		vim.g.clipboard = {
			name = "win32yank-wsl",
			copy = {
				["+"] = win32yank .. " -i --crlf",
				["*"] = win32yank .. " -i --crlf",
			},
			paste = {
				["+"] = win32yank .. " -o --lf",
				["*"] = win32yank .. " -o --lf",
			},
			cache_enabled = 0,
		}
	elseif executable_in_wsl(clip) and executable_in_wsl(powershell) then
		vim.g.clipboard = {
			name = "clip-wsl",
			copy = {
				["+"] = clip,
				["*"] = clip,
			},
			paste = {
				["+"] = powershell .. " -noprofile -command Get-Clipboard",
				["*"] = powershell ..  " -noprofile -command Get-Clipboard",
			},
			cache_enabled = 0,
		}
	else
		vim.notify("未找到可用的剪贴板工具（win32yank 或 clip.exe）", vim.log.levels.WARN)
	end
elseif is_linux then
	-- Linux 虚拟机或物理机
	vim.g.clipboard = {
		name = "LinuxClipboard",
		copy = {
			["+"] = "xclip -selection clipboard",
			["*"] = "xclip -selection primary",
		},
		paste = {
			["+"] = "xclip -selection clipboard -o",
			["*"] = "xclip -selection primary -o",
		},
		cache_enabled = 0,
	}
elseif is_mac then
	-- macOS
	vim.g.clipboard = {
		name = "MacClipboard",
		copy = {
			["+"] = "pbcopy",
			["*"] = "pbcopy",
		},
		paste = {
			["+"] = "pbpaste",
			["*"] = "pbpaste",
		},
		cache_enabled = 0,
	}
end

-- vim.o.clipboard = "unnamedplus"
-- if vim.fn.has('wsl') == 1 then
--     vim.g.clipboard = {
--         name = 'WslClipboard',
--         copy = {
--             ['+'] = 'xclip -selection clipboard',
--             ['*'] = 'xclip -selection clipboard',
--         },
--         paste = {
--             ['+'] = 'xclip -selection clipboard -o',
--             ['*'] = 'xclip -selection clipboard -o',
--         },
--         cache_enabled = 0,
--     }
-- end
--
-- if vim.fn.has('wsl') == 1 then
--     vim.g.clipboard = {
--         name = 'WslClipboard',
--         copy = {
--             ['+'] = '/mnt/c/Windows/System32/clip.exe',
--             ['*'] = {'tmux', 'load-buffer', '-'},
--         },
--         paste = {
--             ['+'] = '/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -c [Console]::Out.Write($(Get-Clipboard -Raw).tostring().replace("`r", ""))',
--             ['*'] = {'tmux', 'save-buffer', '-'},
--         },
--         cache_enabled = '0',
--     }
-- end
